#include "connector.h"
#include <QDebug>
#include <QAbstractSocket>
#include <QList>
#include <QSslCertificate>
#include <QSslError>

#include "core/core.h"

Connector::Connector(QObject *parent) :
	QSslSocket(parent)
{

	connect(this, SIGNAL(encryptedBytesWritten(qint64)),
	   this, SLOT(slot_encryptedBytesWritten(qint64)));
	connect(this, SIGNAL(modeChanged(QSslSocket::SslMode)),
	   this, SLOT(slot_modeChanged(QSslSocket::SslMode)));
	connect(this, SIGNAL(peerVerifyError(const QSslError &)),
	   this, SLOT(slot_peerVerifyError (const QSslError &)));

	bool ignoreErrors = Core::instance()->settings()->value(
			"general/network/sslErrorsIgnore", QVariant(false) ).toBool();

	if( ignoreErrors ){
		connect(this, SIGNAL(sslErrors(const QList<QSslError> &)),
				this, SLOT(ignoreSslErrors()));
	}else{
		connect(this, SIGNAL(sslErrors(const QList<QSslError> &)),
				this, SLOT(slot_sslErrors(const QList<QSslError> &)));
	}

	connect(this, SIGNAL(readyRead()),
	   this, SLOT(slot_readyRead()));
	connect(this, SIGNAL(connected()),
	   this, SLOT(slot_connected()));
	connect(this, SIGNAL(disconnected()),
	   this, SLOT(slot_disconnected()));
	connect(this, SIGNAL(error(QAbstractSocket::SocketError)),
	   this, SLOT(slot_error(QAbstractSocket::SocketError)));
	connect(this, SIGNAL(hostFound()),
	   this, SLOT(slot_hostFound()));
	connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &,
													 QAuthenticator *)),
	   this, SLOT(slot_proxyAuthenticationRequired(const QNetworkProxy &,
												   QAuthenticator *)));
	connect(this, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
	   this, SLOT(slot_stateChanged(QAbstractSocket::SocketState)));
}


void Connector::slot_encrypted (){
   qDebug("Connector::slot_encrypted");
}

void Connector::slot_encryptedBytesWritten (qint64 written){
   qDebug("Connector::slot_encryptedBytesWritten(%ld)", (long) written);
}

void Connector::slot_modeChanged (QSslSocket::SslMode mode){
   qDebug("Connector::slot_modeChanged(%d)", mode);
}

void Connector::slot_peerVerifyError (const QSslError &a){
   qDebug()<<"Connector::slot_peerVerifyError"<< a;
}

void Connector::slot_sslErrors (const QList<QSslError> &a){
//ignoreSslErrors();
   qDebug()<<"Connector::slot_sslErrors"<<a.count()<<a;
   for( int i = 0; i < a.count(); i++ ){
	   qDebug()<< i <<":"<< a.at(i).error() <<a.at(i).errorString()
	   << a.at(i).certificate().isValid();
   }
}

void Connector::slot_readyRead (){
   qDebug("Connector::slot_readyRead");
   qDebug()<<this->readAll();
}

void Connector::slot_connected (){
   qDebug("Connector::slot_connected");
}

void Connector::slot_disconnected (){
   qDebug("Connector::slot_disconnected");
}

void Connector::slot_error (QAbstractSocket::SocketError err){
   qDebug() << "Connector::slot_error(" << err << ")";
   qDebug() << this->errorString();
}

void Connector::slot_hostFound (){
   qDebug("Connector::slot_hostFound");
}

void Connector::slot_proxyAuthenticationRequired (const QNetworkProxy &,
												  QAuthenticator *)
{
   qDebug("Connector::slot_proxyAuthenticationRequired");
}

void Connector::slot_stateChanged(QAbstractSocket::SocketState state){
   qDebug() << "Connector::slot_stateChanged(" << state << ")";
}
